package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.model.IModel;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/catalog")

public class CatalogController {

    @Autowired
    private CatalogService catalogService;



    @GetMapping("/view")
    public String view(){
        return "catalog/main.html";
    }

    @GetMapping("/viewCategory")
    public String viewCategory(String categoryId, Model model){
        if (categoryId != null){
            Category category = catalogService.getCategory(categoryId);
            List<Product> productList = catalogService.getProductListByCategory(categoryId);
            model.addAttribute("category",category);
            model.addAttribute("productList",productList);
        }
        return "catalog/category";
    }

    @GetMapping("/viewProduct")
    public String viewProduct(String productId, Model model) {
        if (productId != null) {
            List<Item> itemList = catalogService.getItemListByProduct(productId);
            Product product = catalogService.getProduct(productId);
            model.addAttribute("itemList",itemList);
            model.addAttribute("product",product);
        }
        return "catalog/product";
    }

    @GetMapping("/viewItem")
    public String viewItem(String itemId, Model model) {
            Item item = catalogService.getItem(itemId);
            Product product = item.getProduct();
            model.addAttribute("item", item);
            model.addAttribute("product", product);
            return "catalog/item";
    }

    @PostMapping("/viewSearch")
    public String searchProducts(String keyword, Model model) {
        if(keyword == null || keyword.length() < 1){
            String msg = "Please enter a keyword to search for, then press the search button.";
            model.addAttribute("msg",msg);
            return "common/error";
        }else {
            List<Product> productList = catalogService.searchProductList(keyword.toLowerCase());
            processProductDescription(productList);
            model.addAttribute("productList",productList);
            return "catalog/searchProduct";
        }
    }


    private void processProductDescription(Product product){
        String [] temp = product.getDescription().split("\"");
        product.setDescriptionImage(temp[1]);
        product.setDescriptionText(temp[2].substring(1));
    }
    private void processProductDescription(List<Product> productList){
        for(Product product : productList) {
            processProductDescription(product);
        }
    }


    @RequestMapping("/search")
    @ResponseBody
    public void  search(HttpServletRequest request, HttpServletResponse response, @RequestParam String keyword) throws IOException {

        System.out.println(keyword);
        List<Product> pList = catalogService.getSearchInfo(keyword);
        String res = "";
        System.out.println(pList.size());
        for (int i = 0; i<pList.size();i++) {
            res += "," + pList.get(i).getName();
        }
        PrintWriter out = response.getWriter();
        out.write(res);
        out.flush();
        out.close();
    }
    @GetMapping("/categoryShow")
    @ResponseBody
    public void categoryShow (HttpServletRequest request, HttpServletResponse response,@RequestParam String categoryId) throws IOException {
        System.out.println(categoryId);
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        String resp = "Product ID      Name\n";
        int i = 0;
        while(i < ((List) productList).size()){
            Product product = productList.get(i);
            resp += product.getProductId() + "      " + product.getName() + "\n";
            i++;
        }
        response.setContentType("text/xml");
        PrintWriter out = response.getWriter();
        out.write(resp);
        out.flush();
        out.close();
    }







}
